package com.meowcook.controller;

import com.meowcook.config.AppConfig;
import com.meowcook.common.AppResult;
import com.meowcook.pojo.dto.CommentDTO;
import com.meowcook.pojo.dto.CommentListDTO;
import com.meowcook.pojo.vo.CommentVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-05-17
 * Time: 16:52
 */
@Slf4j
@Api(tags = "评论接口")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private ICommentService commentService;

    @PostMapping("/addComment")
    @ApiOperation("发表评论")
    public AppResult addComment(HttpServletRequest request, @Valid @RequestBody CommentDTO comment) {
        // 获取用户id，并赋值
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        comment.setUserId(user.getId());
        log.info("评论信息: {}", comment);
        //调用Service
        commentService.addComment(comment);
        return AppResult.success("评论成功");
    }

    @PostMapping("/getCommentList")
    @ApiOperation("获取评论列表")
    public AppResult<List<CommentVO>> getCommentList(@Valid @RequestBody CommentListDTO comment) {
        log.info("评论列表: {}", comment);
        List<CommentVO> commentList = commentService.getCommentList(comment);
        return AppResult.success(commentList);
    }
}
